/**
 * https://leetcode-cn.com/problems/implement-strstr/submissions/
 * 实现indexOf
 * @param haystack 
 * @param needle 
 */
const indexOf = (haystack: string, needle: string): number => {
    // target是空串, 
    if (needle === '') return 0;

    // 源字符串是空串
    if (haystack === '') return -1

    const len = haystack.length;
    // 从头开始遍历
    for (let i = 0; i < len; i++) {
        // 首字符不匹配
        if (haystack[i] !== needle[0]) continue

        // 满足字符串
        if (haystack.substr(i, i + needle.length) === needle) return i
    }

    // 找不到匹配的
    return -1
};